package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.util.PageData;
import cn.wolfcode.rbac.util.QueryConstanst;
import cn.wolfcode.rbac.util.R;
import cn.wolfcode.rbac.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/departments")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @GetMapping
    @RequirePermission(name = "部门列表",expression = "department:query")
    @PreAuthorize("hasAnyAuthority('department:query')")
    public R query(QueryObject qo){
        PageInfo<Department> pageInfo = departmentService.selectList(qo);
        return R.ok(new PageData(pageInfo));
    }
    @DeleteMapping("/{id}")
    @RequirePermission(name = "部门删除",expression = "department:delete")
    @PreAuthorize("hasAnyAuthority('department:delete')")
    public R delete(@PathVariable("id") Long id){
        departmentService.deleteByPrimaryKey(id);
        return R.ok();
    }
    @PostMapping
    @RequirePermission(name = "部门新增",expression = "department:add")
    @PreAuthorize("hasAnyAuthority('department:add')")
    public R add(@RequestBody Department department){
        departmentService.insert(department);
        return R.ok();
    }
    @PutMapping("/{id}")
    @RequirePermission(name = "部门更新",expression = "department:update")
    @PreAuthorize("hasAnyAuthority('department:update')")
    public R update(@PathVariable("id") Long id,@RequestBody Department department){
        departmentService.update(id,department);
        return R.ok();
    }
    @GetMapping(headers = QueryConstanst.CMD_LIST)
    public R list(){
        List<Department> departments = departmentService.selectAll();
        return R.ok(departments);
    }
}
